package com.gs.core.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gs.core.domain.Audit;
import com.gs.core.repository.AuditRepository;
import com.gs.core.service.AuditService;

@Service
@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
public class AuditServiceImpl implements AuditService{
	
	@Autowired
	AuditRepository auditRepository;
	
	public List<Audit> findAll() {
		return auditRepository.findAll();
	}

	public Audit findById(Long id) {
		return auditRepository.findOne(id);
	}
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public Audit saveOrUpdate(Audit entity) {
		if(entity.getId()==null){
			entity.setRecordedDate(new Date());
		}
		return auditRepository.saveAndFlush(entity);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void delete(Audit entity) {
		auditRepository.delete(entity);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void deleteAll() {
		auditRepository.deleteAll();	
	}

}
